package de.fhkl.android.utils;

import java.io.IOException;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.AbstractHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import android.util.Log;

public class ConnectionManager {
	private static String tag = "ConnctionManager";

	public static String get(String url) {
		HttpGet httpRequest = new HttpGet(url);
		HttpClient httpclient = new DefaultHttpClient();
		String result = null;
		try {
			HttpResponse httpResponse = httpclient.execute(httpRequest);
			if (httpResponse.getStatusLine().getStatusCode() == 200) {

				result = EntityUtils.toString(httpResponse.getEntity());
				result = eregi_replace("(\r\n|\r|\n|\n\r)", "", result);

			} else {
				Log.e(tag, "Error Response: "
						+ httpResponse.getStatusLine().toString());
			}
		} catch (Exception e) {
			Log.e(tag, e.getMessage().toString());
			e.printStackTrace();
		}
		return result;
	}

	public static String post(String url, List<NameValuePair> nameValuePairs) {
		HttpPost httpPost = new HttpPost(url);
		DefaultHttpClient httpClient = new DefaultHttpClient();
		String result = null;
		try {
			httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
			Log.w("post", "para=" + nameValuePairs.get(0).getName()
					+ nameValuePairs.get(0).getValue());
			HttpResponse httpResponse = httpClient.execute(httpPost);
			if (httpResponse.getStatusLine().getStatusCode() == 200) {
				result = EntityUtils.toString(httpResponse.getEntity());
				result = eregi_replace("(\r\n|\r|\n|\n\r)", "", result);
			} else {
				Log.i(tag, "Error Response: "
						+ httpResponse.getStatusLine().toString());
			}

		} catch (ClientProtocolException e) {
			Log.e(tag, e.getMessage().toString());
		} catch (IOException e) {
			Log.e(tag, e.getMessage().toString());
		}
		return result;
	}

	public static List<Cookie> postForCookies(String url,
			List<NameValuePair> nameValuePairs) {
		HttpPost httpPost = new HttpPost(url);
		DefaultHttpClient httpClient = new DefaultHttpClient();
		String result = null;
		List<Cookie> cookies = null;
		try {
			httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
			Log.w("postForCookies", "url : " + url);
			for (int i = 0; i < nameValuePairs.size(); i++) {
				Log.w("postForCookies", "setPara : "
						+ nameValuePairs.get(i).getName() + "="
						+ nameValuePairs.get(i).getValue());
			}
			HttpResponse httpResponse = httpClient.execute(httpPost);
			Log.w("postForCookies", "getStatusCode() : "
					+ httpResponse.getStatusLine().getStatusCode());
			if (httpResponse.getStatusLine().getStatusCode() == 200) {
				result = EntityUtils.toString(httpResponse.getEntity());
				result = eregi_replace("(\r\n|\r|\n|\n\r)", "", result);
				cookies = httpClient.getCookieStore().getCookies();
				for (int i = 0; i < nameValuePairs.size(); i++) {
					Log.w("postForCookies", "getPara : "
							+ nameValuePairs.get(i).getName() + "="
							+ nameValuePairs.get(i).getValue());
				}
			} else {
				Log.i(tag, "Error Response: "
						+ httpResponse.getStatusLine().toString());
			}

		} catch (ClientProtocolException e) {
			Log.e(tag, e.getMessage().toString());
		} catch (IOException e) {
			Log.e(tag, e.getMessage().toString());
		}
		return cookies;
	}

	public static BasicNameValuePair getCookie(List<Cookie> cookies, String name) {
		String cookie = null;
		for (int i = 0; i < cookies.size(); i++) {
			if (cookies.get(i).getName().equals((name))) {
				cookie = cookies.get(i).getValue();
			}
		}
		return new BasicNameValuePair(name, cookie);
	}

	public static String eregi_replace(String strFrom, String strTo,
			String strTarget) {
		String strPattern = "(?i)" + strFrom;
		Pattern p = Pattern.compile(strPattern);
		Matcher m = p.matcher(strTarget);
		if (m.find()) {
			return strTarget.replaceAll(strFrom, strTo);
		} else {
			return strTarget;
		}
	}
}
